


from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods,SKU


class OrderSimpleModelSerializer(serializers.ModelSerializer):
    # 重写类属性，覆盖原有映射
    # create_time = serializers.DateTimeField(format="%Y-%m-%d")

    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'create_time',

            'status' # 用于状态修改的校验字段
        ]

        extra_kwargs = {
            'create_time': {'format': '%Y-%m-%d'},
            # 'status': {'min_value': 1, 'max_value': 6, 'required': True}
            'status': {'required': True}
        }


class SKUSimpleModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = [
            'name',
            'default_image'
        ]


class OrderGoodsModelSerializer(serializers.ModelSerializer):

    # 类型：SKU
    # 一个
    sku = SKUSimpleModelSerializer(read_only=True)

    class Meta:
        model = OrderGoods
        fields = [
            'count',
            'price',
            'sku'
        ]


class OrderDetailModelSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField()

    # 数据类型？！一个还是多个？
    # OrderGoods类型；多个数据对象！
    skus = OrderGoodsModelSerializer(many=True, read_only=True)

    class Meta:
        model = OrderInfo
        fields = "__all__"